package com.ellecity06.cwhiskymall.adapter

import android.graphics.Typeface
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.AbsoluteSizeSpan
import android.text.style.StyleSpan
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.text.buildSpannedString
import androidx.core.text.scale
import com.ellecity06.common.base.BaseAdapter
import com.ellecity06.common.ktx.colorResToColorId
import com.ellecity06.common.ktx.dp2px
import com.ellecity06.common.ktx.load
import com.ellecity06.common.ktx.prefixUnit
import com.ellecity06.common.ktx.toAmount
import com.ellecity06.cwhiskymall.R
import com.ellecity06.cwhiskymall.bean.TagItem
import com.ellecity06.cwhiskymall.bean.response.GoodResponse
import com.ellecity06.cwhiskymall.databinding.ItemGoodGridBinding
import com.ellecity06.cwhiskymall.storage.UserStorage
import com.ellecity06.cwhiskymall.view.span.RadiusBackgroundSpan
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent

/**
 * @author ellecity06
 * @time 2024/10/24 20:32
 * @des
 */
class GoodsAdapter : BaseAdapter<GoodResponse, ItemGoodGridBinding>() {
    private val tagList: List<TagItem> by lazy {
        mutableListOf(
            TagItem("正品保证"),
            TagItem("极速发货"),
            TagItem("售后无忧")
        )
    }

    override fun inflateBinding(layoutInflater: LayoutInflater, parent: ViewGroup, viewType: Int): ItemGoodGridBinding {
        return ItemGoodGridBinding.inflate(layoutInflater, parent, false)
    }

    override fun isFullSpanItem(itemType: Int): Boolean {
        if (itemType != 0) return true
        return super.isFullSpanItem(itemType)
    }

    override fun convert(binding: ItemGoodGridBinding, position: Int, item: GoodResponse) {
        binding.ivGood.load(context, item.mainImage)
        binding.tvGoodName.text = if (item.goodsSource == 0) setGoodName(item) else item.spuName
        binding.tvPrice.text = item.contribution
        if (UserStorage.isHasUp()){
            binding.tvPrice.text = buildSpannedString {
                scale(0.7F) {
                    append("赠送 ")
                }
                append(item.contribution?.toAmount())
                scale(0.7F) {
                    append(" 贡献金")
                }
            }
        }else {
            binding.tvPrice.text = "¥${item.salesPrice}"
        }
        binding.tvPrice.visibility = UserStorage.upVisible()
        binding.tvSalePrice.text = "售价 ${item.salesPrice?.toAmount()?.prefixUnit()}"
        setTagData(binding)
    }

    private fun setTagData(binding: ItemGoodGridBinding) {
        val layoutManager = FlexboxLayoutManager(context)
        layoutManager.flexDirection = FlexDirection.ROW
        layoutManager.justifyContent = JustifyContent.FLEX_START
        binding.rvTag.layoutManager = layoutManager
        val goodTagAdapter = GoodTagAdapter()
        binding.rvTag.adapter = goodTagAdapter
        goodTagAdapter.submitList(tagList)
    }

    private fun setGoodName(item: GoodResponse?): SpannableStringBuilder {
        val tag = "自营"
        val goodName = tag + " " + item?.spuName
        val builder = SpannableStringBuilder(goodName)
        builder.setSpan(
            RadiusBackgroundSpan(R.color.colorAccent.colorResToColorId(), 4),
            0,
            tag.length,
            Spannable.SPAN_EXCLUSIVE_INCLUSIVE
        )
        // 改变字体大小
        builder.setSpan(AbsoluteSizeSpan(17f.dp2px()), tag.length, goodName.length, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
        builder.setSpan(StyleSpan(Typeface.BOLD), tag.length, goodName.length, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
        return builder
    }
}